package com.ganzalang.gmall.arthasdemo.controller;

import com.ganzalang.gmall.arthasdemo.service.CalculateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @description: 计算controller
 * @author: Ganzalang
 * @Date 2021/10/13 11:18
 */
@RestController
@Slf4j
@RequestMapping("/discount")
public class CalculateController {

    @Autowired
    private CalculateService calculateService;

    @GetMapping("/calculate")
    public BigDecimal calculateDiscountAmount(BigDecimal amount1, BigDecimal amount2) {
        log.info("入参amount1={}, amount2={}", amount1, amount2);
        if (amount1.compareTo(BigDecimal.ZERO) == 0) {
            throw new ArithmeticException();
        } else if (amount1 == null) {
            throw new NullPointerException();
        }
        BigDecimal discountAmount = calculateService.calculate(amount1, amount2);
        log.info("结果discountAmount={}", discountAmount);
        return discountAmount;
    }
}
